// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "nlohmann/json.hpp"
using json = nlohmann::json;

#include "pugixml/pugixml.hpp"

namespace pugi
{
	template<typename T>
	inline T get_as(const xml_node& elem, const char_t* at, T&& def)
	{
		pugi::xml_attribute atObj = elem.attribute(at);
		if (atObj)
		{
			// Calls from_json() indirectly for string/json -> T conversion.
			json jsonObj = atObj.as_string();
			return jsonObj.get<T>();
		}
		return def;
	}

	template<>
	inline std::string get_as<std::string>(const xml_node& elem, const char_t* at, std::string&& def)
	{
		// string
		return std::string(elem.attribute(at).as_string(def.c_str()));
	}

	template<>
	inline int get_as<int>(const xml_node& elem, const char_t* at, int&& def)
	{
		// int32
		return elem.attribute(at).as_int(std::move(def));
	}

	template<>
	inline double get_as<double>(const xml_node& elem, const char_t* at, double&& def)
	{
		// float64
		return elem.attribute(at).as_double(std::move(def));
	}

	template<>
	inline bool get_as<bool>(const xml_node& elem, const char_t* at, bool&& def)
	{
		// 1*, t* (true), T* (True), y* (yes), Y* (YES)
		return elem.attribute(at).as_bool(std::move(def));
	}

	template<>
	inline std::int64_t get_as<std::int64_t>(const xml_node& elem, const char_t* at, std::int64_t&& def)
	{
		// int64
		return elem.attribute(at).as_llong(std::move(def));
	}
}
